<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table
				:data="accountList.filter(data => !search || data.account.toLowerCase().includes(search.toLowerCase()))"
				v-loading="listLoading"
				border
				row-key="_id"
				:tree-props="{children: 'children', hasChildren: 'hasChildren'}"
				@selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="accountName" label="姓名" min-width="120" sortable>
			</el-table-column>
			<el-table-column prop="account" label="账号" min-width="120" sortable>
			</el-table-column>
			<el-table-column prop="accountPassword" label="密码" min-width="180" sortable>
			</el-table-column>
			<el-table-column label="操作" width="250">
				<template slot="header" slot-scope="scope">
					<el-input
							v-model="search"
							size="mini"
							placeholder="输入账号搜索"/>
				</template>
				<template slot-scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<!--<el-col :span="24" class="toolbar">-->
			<!--<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>-->
			<!--<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="20" :total="total" style="float:right;">-->
			<!--</el-pagination>-->
		<!--</el-col>-->

		<!--编辑界面-->
		<el-dialog title="编辑" :visible.sync="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
				<el-form-item label="上级" prop="name">
					<el-cascader :props="{ checkStrictly: true }" :options="options" @change="cascaderChange($event)" v-model="superiorId" :show-all-levels="false"></el-cascader>
				</el-form-item>
				<el-form-item label="姓名" prop="name">
					<el-input v-model="editForm.accountName" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="账号" prop="name">
					<el-input v-model="editForm.account" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="密码">
					<el-input  v-model="editForm.accountPassword"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

		<!--新增界面-->
		<el-dialog title="新增" :visible.sync="addFormVisible" :close-on-click-modal="false">
			<el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
				<el-form-item label="上级" prop="name">
					<el-cascader :props="{ checkStrictly: true }" :options="options" v-model="superiorId" :show-all-levels="false"></el-cascader>
				</el-form-item>
				<el-form-item label="姓名" prop="name">
					<el-input v-model="addForm.accountName" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="账号" prop="name">
					<el-input v-model="addForm.account" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="密码">
					<el-input  v-model="addForm.accountPassword"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	import { selectFormat } from "@/libs/format";
	import {addAccount,editAccount,getAccountList,delAccount} from "@/api/account/api";
	export default {
		data() {
			return {
				filters: {
					name: ''
				},
				accountList: [],
				listLoading: false,
				sels: [],//列表选中列
				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
				options:[],
				editFormRules: {
					account: [
						{ required: true, message: '请输入账号', trigger: 'blur' }
					],
					accountName: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					],
				},
				//编辑界面数据
				editForm: {
					superiorId:'', // 上级id
					accountName:'',  //账号名
					account: '',   //账号
					accountPassword: '',  //账号密码
					id: '', // id
					userId: JSON.parse(sessionStorage.getItem('user'))._id
				},
				addFormVisible: false,//新增界面是否显示
				addLoading: false,
				addFormRules: {
					account: [
						{ required: true, message: '请输入账号', trigger: 'blur' }
					],
					accountName: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					],
				},
				//新增界面数据
				superiorId:[],  // 上级id
				addForm: {
					superiorId:'', // 上级id
					accountName:'',  //账号名
					account: '',   //账号
					accountPassword: '',  //账号密码
					userId: JSON.parse(sessionStorage.getItem('user'))._id
				},
				search: ''
			}
		},
		methods: {
			searchChange(){
				this.accountList = this.accountList.filter(data => !this.search || data.account.toLowerCase().includes(this.search.toLowerCase()))
			},
			handleCurrentChange(val) {
				this.page = val;
				this.getAccount();
			},
			//获取下拉列表
			getOptionsList() {
				const map = {
					label: 'accountName',
					value: '_id',
					children: 'children'
				}
				this.options = selectFormat.coverTree(this.accountList, map)
				this.options.unshift({
					label: '0',
					value: '0',
				})
			},
			cascaderChange(value){
				console.log(value)
			},
			//获取用户列表
			getAccount() {
				let para = {
					userId: JSON.parse(sessionStorage.getItem('user'))._id
				};
				this.listLoading = true;
				getAccountList(para).then((res) => {
					this.accountList = res.data;
					this.getOptionsList()
					this.listLoading = false;
				});
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					let para = { id: row._id };
					delAccount(para).then((res) => {
						this.listLoading = false;
						this.getAccount();
					});
				}).catch(() => {

				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
				this.superiorId = []
				this.editFormVisible = true;
				this.editForm.account = row.account;
				this.editForm.accountName = row.accountName;
				this.editForm.accountPassword = row.accountPassword;
				this.editForm.superiorId = row.superiorId;
				this.superiorId.push(row.superiorId)
				this.editForm.id = row._id;
			},
			//显示新增界面
			handleAdd: function () {
				this.superiorId = []
				this.addFormVisible = true;
			},
			//编辑
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true;
							if(this.superiorId[this.superiorId.length - 1]){
								this.editForm.superiorId = this.superiorId[this.superiorId.length - 1]
							}else{
								this.editForm.superiorId = '0'
							}
							let para = Object.assign({}, this.editForm);
							editAccount(para).then((res) => {
								if(res.code == 200) {
									this.editLoading = false;
									this.editFormVisible = false;
									this.getAccount();
								}
							});
						});
					}
				});
			},
			//新增
			addSubmit: function () {
				this.$refs.addForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.addLoading = true;
							if(this.superiorId[this.superiorId.length - 1]){
								this.addForm.superiorId = this.superiorId[this.superiorId.length - 1]
							}else{
								this.addForm.superiorId = '0'
							}
							if(!this.addForm.userId){
								this.$message.error('请刷新重试！	')
								return false
							}
							let para = Object.assign({}, this.addForm);
							addAccount(para).then((res) => {
								if(res.code == 200){
									Object.keys(this.addForm).forEach(key => this.addForm[key] = '');
									this.addLoading = false;
									this.addFormVisible = false;
									this.getAccount()
								}
							});
						});
					}
				});
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id).toString();
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					let para = { ids: ids };
					batchRemoveUser(para).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getAccount();
					});
				}).catch(() => {

				});
			}
		},
		mounted() {
			this.getAccount();
		}
	}

</script>

<style scoped>

</style>
